const {BrowserWindow} = require('electron').remote
const path = require('path')
const io = require('./io');
const Page = require('./pagination');
var async = require('async');

io.websocketemit('allorder', null);

//"{"desc":"ddd","endtime":"2018-12-05","ids":["C305B00CA850AE41F1BB19951AFFC1D8","F94B9E4EBDA98A94F7857F95C76D9F70"],"orderid":"ITJ6QoRlymiu3Vk3vETMjRs3Fmyqhjon","user":"rtl","state":0}"

var AllOrders;
var mState = -1;



io.onwebsocket('allorder', (data) => {
    AllOrders = data;
    SetupList();
    
});

function formatOrder(user,orders,callback){
    if(orders == null){
        return;
    }
    var orderTemplate = "<tr class='success'>\
                            <td><input value={6} type='text' readonly='readonly' style='width:100%;height:100%;margin:0px;padding:0px;' /></td>\
                            <td><input value={0} type='text' readonly='readonly' style='width:100%;height:100%;margin:0px;padding:0px;' /></td>\
                            <td>{1}</td>\
                            <td>{2}</td>\
                            <td>{3}</td>\
                            <td><button class='btnItemDetail' user='{0}' order='{6}' >查看</button></td>\
                        </tr>";
                   
    var htmls = "";     
    async.forEach(Object.keys(orders),(o,cb)=>{
        if(mState != -1 && mState != o.state){
            cb(null);
        }else{
            var order = JSON.parse(orders[o]);
            var stateClass = "待处理";
            var theme = 'info';
            var btnFinishHtml = "<button user='{0}' order='{1}'>完成</button>".format(user.user, o);
            if(order.state == 1){
                stateClass = "已完成";      
                btnFinishText = "";
                theme = 'success'; 
            }
            htmls += orderTemplate.format(user.user,order.desc,order.endtime,stateClass, btnFinishHtml, theme,o);
            cb(null);
        }
    },(err)=>{
        callback(htmls);
    })
}


function SetupList(){
    var htmls = ""; 
    async.forEach(Object.keys(AllOrders),(key,cb)=>{
        var item = AllOrders[key];
        formatOrder(item.user,item.orders,(s)=>{
            htmls += s;
            cb(null);
        });
    },(err)=>{
        document.getElementById("orderfiles").innerHTML = htmls;

        $("button.btnItemDetail").bind("click", function(){
            var order = $(this).attr("order");
            var user = $(this).attr("user");
            const crashWinPath = path.join('file://', __dirname, '../sections/orderdetail.html?orderid=' +order+'&user='+user)    
            let win = new BrowserWindow({ width: 1000, height: 800, minimizable:false, maximizable:false,alwaysOnTop:true, title:"订单详情" })
            win.setMenu(null)
            win.on('close', () => { win = null })
            win.loadURL(crashWinPath);
            win.show()

            // win.webContents.openDevTools()
            // win.maximize()
            // require('devtron').install();
        });
    })
}

function SelectStateChanged(){
    mState = document.getElementById("selectOrderState").value;
    SetupList();
}

$("select#selectOrderState").bind("click", function(){
    SelectStateChanged();
});

$("#btnRefresh").bind("click", function(){
    io.websocketemit('allorder', null);
});

